import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store/index';

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch((err) => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
    {
        path: '/',
        name: '军政宝',
        component: () => import('../views/home/index'),
    },

    {
        path: '/search',
        name: '搜索',
        component: () => import('../views/search/search'),
    },

    {
        path: '/shop',
        name: '商城',
        component: () => import('../views/shop/shop'),
    },
    {
        path: '/shopDetail',
        name: '商城详情',
        component: () => import('../views/shop/shopDetail'),
    },
    {
        path: '/shopCart',
        name: '购物车',
        component: () => import('../views/shop/shopCart'),
    },
    {
        path: '/shopSubmit',
        name: '填写订单',
        component: () => import('../views/shop/shopSubmit'),
    },
    {
        path: '/payOrder',
        name: '订单支付',
        component: () => import('../views/shop/payOrder'),
    },
    {
        path: '/success',
        name: '提示',
        component: () => import('../views/success/index'),
    },
    {
        path: '/allOrder',
        name: '我的订单',
        component: () => import('../views/order/allOrder'),
    },
    {
        path: '/orderDiscuss',
        name: '我的评价',
        component: () => import('../views/order/orderDiscuss'),
    },
    {
        path: '/dicuss',
        name: '评价',
        component: () => import('../views/order/dicuss'),
    },
    {
        path: '/disscussDetail',
        name: '评价详情',
        component: () => import('../views/order/disscussDetail'),
    },
    {
        path: '/orderDetial',
        name: '订单详情',
        component: () => import('../views/order/orderDetial'),
    },

    {
        path: '/zixun',
        name: '资讯',
        component: () => import('../views/zixun/zixun'),
    },
    {
        path: '/zixunDetail',
        name: '资讯详情',
        component: () => import('../views/zixun/zixunDetail'),
    },

    {
        path: '/login',
        name: '登录',
        component: () => import('../views/login/login'),
    },
    {
        path: '/xieyi',
        name: '服务使用协议',
        component: () => import('../views/login/xieyi'),
    },

    {
        path: '/user',
        name: '个人中心',
        component: () => import('../views/user/user'),
    },
    {
        path: '/Attes',
        name: '军属认证',
        component: () => import('../views/user/Attes'),
    },
    {
        path: '/userinfo',
        name: '设置',
        component: () => import('../views/user/userinfo'),
    },
    {
        path: '/usersetting',
        name: '用户设置',
        component: () => import('../views/user/usersetting'),
    },
    {
        path: '/address_list',
        name: '收货地址列表',
        component: () => import('../views/user/address_list'),
    },
    {
        path: '/address_list_check',
        name: '选择收货地址',
        component: () => import('../views/user/address_list_check'),
    },
    {
        path: '/address_edit',
        name: '新增/编辑收货地址',
        component: () => import('../views/user/address_edit'),
    },
    {
        path: '/about',
        name: '关于我们',
        component: () => import('../views/user/about'),
    },
    {
        path: '/Privacy',
        name: '隐私政策',
        component: () => import('../views/user/Privacy'),
    },
    {
        path: '/swHezuo',
        name: '商务合作',
        component: () => import('../views/user/swHezuo'),
    },
    {
        path: '/alwaysQuestion',
        name: '常见问题',
        component: () => import('../views/user/alwaysQuestion'),
    },
    {
        path: '/message',
        name: '我的消息',
        component: () => import('../views/message/message'),
    },
    {
        path: '/messageList',
        name: '消息列表',
        component: () => import('../views/message/messageList'),
    },
    {
        path: '/paypwd',
        name: '支付密码',
        component: () => import('../views/user/paypwd'),
    },
    {
        path: '/collect',
        name: '我的收藏',
        component: () => import('../views/user/collect'),
    },
    {
        path: '/coupon',
        name: '我的优惠券',
        component: () => import('../views/user/coupon'),
    },

    {
        path: '/peixun',
        name: '教育培训',
        component: () => import('../views/peixun/peixun'),
    },
    {
        path: '/peixunDetial',
        name: '培训详情',
        component: () => import('../views/peixun/peixunDetial'),
    },
    {
        path: '/education',
        name: '学历提升',
        component: () => import('../views/peixun/education'),
    },
    {
        path: '/legal',
        name: '法律援助',
        component: () => import('../views/legal/legal'),
    },
    {
        path: '/insurance',
        name: '金融保险',
        component: () => import('../views/insurance/insurance'),
    },
    {
        path: '/localLife',
        name: '本地生活',
        component: () => import('../views/localLife/localLife'),
    },

    {
        path: '/jobHome',
        name: '招聘',
        component: () => import('../views/Job/jobHome'),
    },
    {
        path: '/jobFind',
        name: '找工作',
        component: () => import('../views/Job/jobFind'),
    },
    {
        path: '/jobFindDetail',
        name: '职位详情',
        component: () => import('../views/Job/jobFindDetail'),
    },
    {
        path: '/companyDetail',
        name: '公司详情',
        component: () => import('../views/Job/companyDetail'),
    },
    {
        path: '/editJianli',
        name: '编辑简历',
        component: () => import('../views/jobUser/editJianli'),
    },

    {
        path: '/companyInfo',
        name: '企业管理',
        component: () => import('../views/company/companyInfo'),
    },
    {
        path: '/publishJob',
        name: '发布职位',
        component: () => import('../views/company/publishJob'),
    },
    {
        path: '/positionManagement',
        name: '职位管理',
        component: () => import('../views/company/positionManagement'),
    },
    {
        path: '/myDodnload',
        name: '我的下载',
        component: () => import('../views/company/myDodnload'),
    },
    {
        path: '/receiveApplication',
        name: '收到申请',
        component: () => import('../views/company/receiveApplication'),
    },
    {
        path: '/InterviewInvitation',
        name: '面试邀请',
        component: () => import('../views/company/InterviewInvitation'),
    },
    {
        path: '/sendMianshi',
        name: '发送面试邀请',
        component: () => import('../views/company/sendMianshi'),
    },
    {
        path: '/zhiweiList',
        name: '求职反馈',
        component: () => import('../views/jobUser/zhiweiList'),
    },
    {
        path: '/personnelFind',
        name: '人才招聘',
        component: () => import('../views/Job/personnelFind'),
    },
    {
        path: '/personDetail',
        name: '简历详情',
        component: () => import('../views/Job/personDetail'),
    },
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});

// 修改store是数据
const changeState = (
    ifHeader = false,
    backText = '',
    isBacknav = true,
    isSearchred = false,
    isfixed = true,
    isInputturn = true,
    placeholder = '请输入搜索内容',
    isInputread = true,
    isreadBack = false,
    isbacknav = true,
    turnLink = ''
) => {
    store.commit('changeheaderInfo', {
        ifHeader: ifHeader, // 是否显示导航组件，默认显示
        backText: backText, // back文本
        isBacknav: isBacknav, // 显示搜索还是后退，默认搜索
        isSearchred: isSearchred, // 显示搜索，红色导航还是黑色导航，默认红色
        isfixed: isfixed, // 导航是否固定在顶部
        isInputturn: isInputturn, // 点击搜索是否跳转到搜索页面，默认是
        placeholder: placeholder, // placeholder文本
        isInputread: isInputread, // input是否只为可读，默认是
        isreadBack: isreadBack, // back是否为红色，默认否
        isbacknav: isbacknav, // 小导航
        turnLink: turnLink, //
    });
};

router.beforeEach((to, from, next) => {
    store.commit('changeSearchval', '');
    let userinfo = localStorage.getItem('userinfo');
    // 检测是否登录
    if (userinfo) {
        next();
    } else {
        switch (to.path) {
            case '/':
            case '/login':
            case '/peixun':
            case '/education':
            case '/Attes':
            case '/shop':
            case '/shopDetail':
            case '/legal':
            case '/Privacy':
            case '/insurance':
            case '/localLife':
            case '/jobHome':
            case '/success':
            case '/xieyi':
                next();
                break;
            default:
                next('/login');
                break;
        }
    }
});

//导航后置守卫，可以在确定导航到目标页面时再更改title
router.afterEach((to) => {
    window.document.title = to.name;
    // 控制页面导航的数据
    switch (to.path) {
        case '/':
            changeState(
                true,
                to.name,
                true,
                false,
                true,
                true,
                '请输入要搜索得内容',
                true,
                false,
                true,
                '/shop'
            ); // 显示 红色搜索框，固定在头部
            break;
        case '/jobHome':
            changeState(
                true,
                to.name,
                true,
                true,
                true,
                true,
                '请输入要搜索得内容',
                true,
                false,
                true,
                '/jobFind'
            );
            break;
        case '/shop':
        case '/zixun':
            changeState(true, to.name); // 显示 红色搜索框，固定在头部
            break;
        case '/login':
            changeState(true, to.name, false, false, false); // 显示、后退、红色搜索、固定
            break;
        case '/shopDetail':
            changeState(true, '', false, false, false); // 显示、后退、红色搜索、固定
            break;
        case '/Attes':
        case '/userinfo':
        case '/usersetting':
        case '/address_list':
        case '/address_list_check':
        case '/about':
        case '/Privacy':
        case '/shopCart':
        case '/shopSubmit':
        case '/payOrder':
        case '/allOrder':
        case '/swHezuo':
        case '/alwaysQuestion':
        case '/message':
        case '/paypwd':
        case '/collect':
        case '/coupon':
        case '/orderDiscuss':
        case '/dicuss':
        case '/disscussDetail':
        case '/education':
        case '/success':
        case '/jobFindDetail':
        case '/companyDetail':
        case '/editJianli':
        case '/publishJob':
        case '/positionManagement':
        case '/myDodnload':
        case '/receiveApplication':
        case '/InterviewInvitation':
        case '/sendMianshi':
        case '/zhiweiList':
        case '/personDetail':
        case '/orderDetial':
        case '/xieyi':
            changeState(
                true,
                to.name,
                false,
                true,
                true,
                true,
                '',
                true,
                false
            ); // 显示、后退、白色搜索、不固定、军属认证
            break;
        case '/address_edit':
        case '/messageList':
            changeState(
                true,
                to.name,
                false,
                true,
                true,
                true,
                '',
                true,
                false,
                false
            ); // 显示、后退、白色搜索、不固定、军属认证、小导航不显示
            break;
        case '/peixun':
        case '/legal':
        case '/insurance':
        case '/localLife':
        case '/jobFind':
        case '/personnelFind':
            changeState(true, to.name, true, true, false); // 显示、搜索、黑色搜索、不固定
            break;
        case '/zixunDetail':
        case '/companyInfo':
        case '/peixunDetial':
            changeState(true, to.name, false, true, true, true, '', true, true); // 显示、后退、白色搜索、不固定、军属认证
            break;
        default:
            changeState(false);
            break;
    }
});

export default router;
